using System;
using System.Data;
using System.Xml;
using System.Runtime.Serialization;
using System.Collections;
using Widgetsphere.Core.Exceptions;
using Widgetsphere.Core.DataAccess;
using ACME.Northwind.Business;
using ACME.Northwind.Business.Objects;
using ACME.Northwind.Business.Rules;
using ACME.Northwind.Business.SelectCommands;
using System.Collections.Generic;

namespace ACME.Northwind.Domain.Objects
{
	/// <summary>
	/// This is an customizable extender for the domain class associated with the 'Shipper' object
	/// </summary>
	[Serializable()]
	partial class DomainShipper : PersistableDomainObjectBase
	{
		#region Member Variables

		private const string TABLE_NAME = "ShipperCollection";
		internal DomainShipperCollection ParentCol;

		#endregion

		#region Constructor

		internal DomainShipper(DataRowBuilder rb) : base(rb) 
		{
			this.ParentCol = ((DomainShipperCollection)(base.Table));
		}

		#endregion

		#region Properties

		internal ShipperPrimaryKey PrimaryKey
		{
			get { return new ShipperPrimaryKey(this.ShipperId); }
		}

		internal string CompanyName
		{
			get
			{
				try
				{
					if (base[this.ParentCol.CompanyNameColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.CompanyNameColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  if (value == null) throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_SETNULL);

			  if (!base[this.ParentCol.CompanyNameColumn].Equals(value))
				{
					base[this.ParentCol.CompanyNameColumn] = value;
			  	wasSet = true;
				}

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ShipperCollection list = (ShipperCollection)this.ParentCol.SubDomain[Collections.ShipperCollection];
					Shipper item = list.GetItemByPK(this.ShipperId);
					if (item != null) item.CompanyName = value;
				}
			}
		}

		internal string Phone
		{
			get
			{
				try
				{
					if (base[this.ParentCol.PhoneColumn] == System.DBNull.Value) return null;
					else return ((string)(base[this.ParentCol.PhoneColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
			set 
			{
				bool wasSet = false;
			  object newValue = DomainHelper.PropertySetterHelper(base[this.ParentCol.PhoneColumn], value, out wasSet);
				if (wasSet) base[this.ParentCol.PhoneColumn] = newValue;

				if (wasSet && (this.ParentCol.TableName != TABLE_NAME) && this.ParentCol.SubDomain.Tables.Contains(TABLE_NAME))
				{
					ShipperCollection list = (ShipperCollection)this.ParentCol.SubDomain[Collections.ShipperCollection];
					Shipper item = list.GetItemByPK(this.ShipperId);
					if (item != null) item.Phone = value;
				}
			}
		}

		internal int ShipperId
		{
			get
			{
				try
				{
					return ((int)(base[this.ParentCol.ShipperIdColumn]));
				}
				catch (InvalidCastException e) { throw new StrongTypingException(GlobalValues.ERROR_PROPERTY_NULL, e); }
			}
		}

		internal virtual BusinessObjectList<Order> OrderList
		{
			get
			{
				BusinessObjectList<Order> associatedOrderList = null;
				//if (associatedOrderList == null)
				//{
					associatedOrderList = new BusinessObjectList<Order>();
					foreach (DomainOrder Order in base.GetChildRows(ParentCol.ShipperOrderRelation))
					{
						if (Order.RowState != DataRowState.Deleted)
							associatedOrderList.Add(new Order(Order));
					}
					associatedOrderList.ObjectAdded += new BusinessObjectEventHandler(AssociatedOrderList_ObjectAdded);
					associatedOrderList.ObjectRemoved += new BusinessObjectEventHandler(AssociatedOrderList_ObjectRemoved);
				//}
				return associatedOrderList;
			}
		}

		#endregion

		#region Collection Operation Methods

		/// <summary>
		/// Serves as a hash function for this particular type.
		/// </summary>
		public override int GetHashCode()
		{
			return base.GetHashCode();
		}

		public override bool Equals(object obj)
		{
			if (obj == null) return false;
			if (obj.GetType() != this.GetType()) return false;

			if ((this.RowState == DataRowState.Deleted) ||
				(((DataRow)obj).RowState == DataRowState.Deleted) ||
				(this.RowState == DataRowState.Detached) ||
				(((DataRow)obj).RowState == DataRowState.Detached))
				return false;

			return ((DomainShipper)obj).PrimaryKey.Equals(this.PrimaryKey);
		}

		public override void Persist()
		{
			DomainHelper.PersistDomainObject(this, this.ParentCol.SubDomain, Collections.ShipperCollection, this.ParentCol.Modifier);
		}

		public override void Remove()
		{
			this.ParentCol.RemoveShipper(this);
		}

		#endregion

		#region Relationship Methods
		private void AssociatedOrderList_ObjectAdded(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainOrder addedItem = (DomainOrder)e.BusinessObject.WrappedClass;
			DomainOrderCollection existingOrderCollection = (DomainOrderCollection)this.ParentCol.SubDomain.GetDomainCollection(Collections.OrderCollection);
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			if(addedItem.ParentCol != existingOrderCollection)
			{
				this.ParentCol.SubDomain.Merge(new DataRow[] { addedItem } , false, MissingSchemaAction.Error);
				addedItem = existingOrderCollection.GetOrder(addedItem.OrderId);
				e.BusinessObject.WrappedClass = addedItem;
			}
			addedItem.SetParentRow(this,this.ParentCol.ShipperOrderRelation);
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		private void AssociatedOrderList_ObjectRemoved(object sender, Widgetsphere.Core.EventArgs.BusinessObjectEventArgs e)
		{
			DomainOrder removedItem = (DomainOrder)e.BusinessObject.WrappedClass;
			bool oldEc = this.ParentCol.SubDomain.EnforceConstraints;
			this.ParentCol.SubDomain.EnforceConstraints = false;
			removedItem.ShipVia = null;
			this.ParentCol.SubDomain.EnforceConstraints = oldEc;
		}

		internal virtual void ReleaseNonIdentifyingRelationships()
		{
			this.OrderList.Clear();
		}

		#endregion
	}
}
